package com.hidao.platform.zike.web.controller;

import com.hidao.basic.common.vo.ResultMap;
import com.hidao.basic.http.util.CookieUtils;
import com.hidao.platform.ssh.web.basic.BaseController;
import com.hidao.platform.ssh.web.basic.WeatherUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping
public class IndexController extends BaseController {


	@RequestMapping(value = { "/index", "/" })
	public String index(Model model) {
		model.addAttribute("pname", "快捷入口");
		return "page/index/index";
	}

	@RequestMapping("/weather/show")
	@ResponseBody
	public Map<String, Object> showWeather(HttpServletRequest request) {
		if (request.getSession().getAttribute("w") != null) {
			return (Map<String, Object>) request.getSession().getAttribute("w");
		}
		String cityName = "深圳";
		ResultMap results = WeatherUtils.findCacheWeather(cityName);
		if (results.isSuccess()) {
			Date timeOut = (Date) results.get("timeOut");
			if (timeOut == null || timeOut.before(new Date())) {
				results = WeatherUtils.updateCacheWeather(cityName);
			}
		}
		request.getSession().setAttribute("w", results);
		return results;
	}

	@RequestMapping(value = "user/login", method = RequestMethod.GET)
	public String login(Model model) {
		return "page/index/login";
	}

	@RequestMapping(value = "user/login", method = RequestMethod.POST)
	@ResponseBody
	public ResultMap login(String username, String password, String verifyCode, @RequestParam(defaultValue = "false") Boolean remember, HttpServletRequest request) {
		ResultMap results = new ResultMap();
		String msg = null;
		UsernamePasswordToken token = new UsernamePasswordToken(username.trim(), password.trim());
		token.setRememberMe(remember);
		try {
			SecurityUtils.getSubject().login(token);
		} catch (UnknownAccountException e) {
			msg = "用户名或者密码不正确";
		} catch (IncorrectCredentialsException e) {
			msg = "用户名或者密码不正确";
		} catch (AuthenticationException e) {
			msg = e.getMessage();
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace();
		}
		if (msg != null) {
			results.putError(msg);
			return results;
		}

		results.putSuccess(request.getContextPath() + "/index");
		return results;
	}

	// 当前用户退出，退出后跳转到首页
	@RequestMapping(value = "user/logout", method = RequestMethod.GET)
	public String logout(HttpServletRequest request, HttpServletResponse response) {
		SecurityUtils.getSubject().logout();
		CookieUtils.delCookie(request, response, "MuserId");
		return "redirect:/index";
	}

}
